#include "bsp.h"

extern SPI_HandleTypeDef hspi1;
extern SPI_HandleTypeDef hspi2;
extern ADC_HandleTypeDef hadc;
extern TIM_HandleTypeDef htim2;
extern UART_HandleTypeDef huart2;
extern uint8_t uartRxBuffer[255]; //声明外部变量
extern DMA_HandleTypeDef hdma_usart2_rx;

struct Tlv5618 dac;

uint16_t bsp_adcVal[8] = {0};

/* ADC channel configuration structure declaration */
ADC_ChannelConfTypeDef sConfig;

/* Converted value declaration */
uint32_t aResultDMA;

void _dacSetCs(uint8_t val)
{
	HAL_GPIO_WritePin(DAC_CS_GPIO_Port, DAC_CS_Pin, val ? GPIO_PIN_SET : GPIO_PIN_RESET);
}

void _dacWriteSpi16(uint16_t val)
{
	static uint8_t arr[2];

	arr[0] = val >> 8;
	arr[1] = val & 0xFF;

	HAL_SPI_Transmit(&hspi1, (uint8_t *)&arr, 2, 0xff);
}

void bsp_dacInitial(void)
{
	tlv5618_init(&dac, _dacSetCs, _dacWriteSpi16);
}

void bsp_setDacCV(double val)
{
	tlv5618_setOutA(&dac, (uint16_t)(val * 2048 / 2.5) - 1);
}

void bsp_setDacCC(double val)
{
	tlv5618_setOutB(&dac, (uint16_t)(val * 2048 / 2.5) - 1);
}

void bsp_adcInitial(void)
{
	/* ### - 2 - Start calibration ############################################ */
	if (HAL_ADCEx_Calibration_Start(&hadc, ADC_SINGLE_ENDED) != HAL_OK) {
		Error_Handler();
	}
}

double bsp_getAdcCV(void)
{
	sConfig.Channel = ADC_CHANNEL_0;
	sConfig.Rank = ADC_RANK_CHANNEL_NUMBER;
	if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK) {
		Error_Handler();
	}

	sConfig.Channel = ADC_CHANNEL_1;
	sConfig.Rank = ADC_RANK_NONE;
	if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK) {
		Error_Handler();
	}

	uint32_t value = 0;
	HAL_ADC_Start(&hadc);
	HAL_ADC_PollForConversion(&hadc, 1000);
	value = HAL_ADC_GetValue(&hadc);
#ifdef DEBUG
	printf("ADC_V reg: 0x%08x, %lu\r\n", (unsigned int)value, value);
#endif

	HAL_ADC_Stop(&hadc);

	return 2.5 * value / 4096;
}

double bsp_getAdcCC(void)
{
	sConfig.Channel = ADC_CHANNEL_0;
	sConfig.Rank = ADC_RANK_NONE;
	if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK) {
		Error_Handler();
	}

	sConfig.Channel = ADC_CHANNEL_1;
	sConfig.Rank = ADC_RANK_CHANNEL_NUMBER;
	if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK) {
		Error_Handler();
	}

	uint32_t value = 0;
	HAL_ADC_Start(&hadc);
	HAL_ADC_PollForConversion(&hadc, 1000);
	value = HAL_ADC_GetValue(&hadc);
#ifdef DEBUG
	printf("ADC_C reg: 0x%08x, %lu\r\n", (unsigned int)value, value);
#endif

	HAL_ADC_Stop(&hadc);

	return 2.5 * value / 4096;
}

void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef *hadc)
{
}

int bsp_PWMEnable(uint8_t state)
{
	if (state != 0) {
		if (HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_1) != HAL_OK) {
			return 1;
		} else {
			return 0;
		}
	} else {
		if (HAL_TIM_PWM_Stop(&htim2, TIM_CHANNEL_1) != HAL_OK) {
			return 1;
		} else {
			return 0;
		}
	}
}
/*
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
	UartIRQFlag = 1;
}*/

void USER_UART_IDLECallback(UART_HandleTypeDef *huart)
{
	HAL_UART_DMAStop(&huart2); //停止本次DMA传输
	UartIRQFlag = 1;
}

void USER_UART_IRQHandler(UART_HandleTypeDef *huart)
{
	if (USART2 == huart->Instance) //判断是否是串口1（！此处应写(huart->Instance == USART1)
	{
		if (RESET != __HAL_UART_GET_FLAG(&huart2, UART_FLAG_IDLE)) //判断是否是空闲中断
		{
			__HAL_UART_CLEAR_IDLEFLAG(&huart2); //清楚空闲中断标志（否则会一直不断进入中断）
			app_dprintf("\r\nUART2 Idle IQR Detected\r\n");
			USER_UART_IDLECallback(huart); //调用中断处理函数
		}
	}
}
